global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

capture log close
log using "$log/5_stayers_distribution", replace


*Loop over 37 months
cap program drop loop
program define loop 
args year month ref end treat event
clear all 

	*Define base month 
		local m=mod(`month'-1+11,12)+1
	    local y=`year'+int((`month'-1+11)/12)
		capture use "$temp\cleaned_`y'`m'", clear
		capture use "$temp\cleaned_`y'0`m'", clear
		
		*Keep only stayers 
		merge m:1 state client_id emp_pur_c using "$temp\balanced_workers_`ref'.dta"
		keep if _merge==3
		
		*Keep main variables 
		rename salaried salaried0
		rename base base0
		keep state client_id emp_pur_c salaried0 base0
		*drop if state=="CA" | state=="NY" | state=="ME" | state=="AK"
		
		tempfile base_stayers
		save `base_stayers'
	
	*Loop over each month, keeping only stayers 
	forvalues i=0/36 {
	    local m=mod(`month'-1+`i',12)+1
	    local y=`year'+int((`month'-1+`i')/12)
		
		*Import
		capture use "$temp\cleaned_`y'`m'", clear
		capture use "$temp\cleaned_`y'0`m'", clear
		
		*Keep base period 
		merge m:1 state client_id emp_pur_c using `base_stayers'
		keep if _merge==3
		drop _merge
		
		*Save for worker panel 
		compress
		save "$temp\workers_`i'.dta", replace
		
		*Collapse 
		gen bin=floor((base-913)/20)*20+913
		keep if bin<=2500
		collapse (count) n=base , by(yr_month client_id salaried0 bin)
		
		*Expand to fill in bin
			gegen group=group(client_id salaried)
			replace bin=(bin-913)/20
			tsset group bin  
			tsfill, full
				
			bysort group: carryforward yr_month, replace
			bysort group: carryforward client_id, replace
			bysort group: carryforward salaried, replace
			
			gsort group -bin 
			bysort group: carryforward yr_month, replace
			bysort group: carryforward client_id, replace
			bysort group: carryforward salaried, replace
				
			drop group
			replace bin=bin*20+913
			
		*Expand to fill in salaried
			gegen group=group(client_id bin)
			tsset group salaried 
			tsfill, full
				
			bysort group: carryforward yr_month, replace
			bysort group: carryforward client_id, replace
			bysort group: carryforward bin, replace
				
			gsort group -salaried 
			bysort group: carryforward yr_month, replace
			bysort group: carryforward client_id, replace
			bysort group: carryforward bin, replace
				
			drop group
			
		*Fill in missing outcomes 
			replace n=0 if n==.
			
		*Save 
			compress
			save "$temp\workers_bin_`i'", replace
	}

	*Append worker level
	clear all 
	forvalues i=0/36 {
		local m=mod(`month'-1+`i',12)+1
	    local y=`year'+int((`month'-1+`i')/12)
	    append using "$temp\workers_`i'.dta"
	}
	save "$output\stayers_panel_`ref'.dta", replace
	
	*Append distribution
	clear all 
	forvalues i=0/36 {
		local m=mod(`month'-1+`i',12)+1
	    local y=`year'+int((`month'-1+`i')/12)
	    append using "$temp\workers_bin_`i'.dta"
	}
	save "$output\stayers_distribution_`ref'.dta", replace
end

loop 2015 5 treat 
loop 2013 5 control 

*Delete temp files
forvalues i=0/36 {
	erase "$temp\workers_`i'.dta"
	erase "$temp\workers_bin_`i'.dta"
}

log close
